package com.algo.count;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Set;
import java.util.Map;
import java.util.HashSet;
import java.util.HashMap;

import com.algo.utils.PanelUtils;
import com.algo.utils.ContainerUtils;

public class CountTitlePopAttr{
        class Value{
                public Set<String> uuids = new HashSet<String>();
                public String title;
                public int[] counter = new int[19];
                public Value(String title){
                        this.title = title;
                }
                public String toString(){
                        return title + "\t" + ContainerUtils.arrayToString(counter);
                }
        }
        public void count(String input1, String input2, String output) throws IOException{
                BufferedReader br1 = new BufferedReader(new FileReader(input1));
                Map<String, Value> urlTitle = new HashMap<String, Value>();
                BufferedReader br2 = new BufferedReader(new FileReader(input2));
                String line;
                while((line = br2.readLine()) != null){
                        String[] items = line.split("\\t");
			if(items.length < 2) {System.out.println(line); continue;}
                        urlTitle.put(items[0], new Value(items[1]));
                }
                br2.close();
		System.out.println("br2 closed");
                while((line = br1.readLine()) != null){
                        String[] items = line.split("\\t");
			if(items.length < 2) {System.out.println(line); continue;}
                        String url = items[1];
                        if(urlTitle.containsKey(url)){
                                Value value = (Value) urlTitle.get(url);
                                int pos = items[0].indexOf(",");
                                String uuid = items[0].substring(0, pos);
                                if(!value.uuids.contains(uuid)){
					value.uuids.add(uuid);
                                        PanelUtils.addPopAttr(value.counter, items[0].substring(pos+1).split(","));
                                }
                        }
                }
                br1.close();
                

                BufferedWriter bw = new BufferedWriter(new FileWriter(output));
                for(Map.Entry<String, Value> entry : urlTitle.entrySet()){
                        bw.write(entry.getKey() + "\t" + entry.getValue() + "\n");
                }
                bw.close();
        }
        public static void main(String[] args) throws IOException{
                new CountTitlePopAttr().count(args[0], args[1], args[2]);
        }

}

